//
//  BSSettingViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/20.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSSettingViewController.h"
#import "BSSettingTableViewCell.h"
#import "SDImageCache.h"
#import "FMDatabase.h"
#import "UMSocial.h"
#import "UMSocialSnsService.h"
#import "BSAboutUsTableViewController.h"
#import "BSPrivacyPolicyViewController.h"

@interface BSSettingViewController ()
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)NSArray *titleArray;
@property (nonatomic, strong)NSString *string;
@property (nonatomic, strong)UIView *tanview;
@end

@implementation BSSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self uiConfig];
    
}

-(void)uiConfig
{
    _dataArray = [[NSMutableArray alloc] init];
    _titleArray = @[@"功能设置", @"其他"];
    
 
                [_dataArray addObject:@[@"字体大小"]];
                [_dataArray addObject:@[@"清除缓存",@"推荐给朋友",@"关于我们",@"隐私政策",@"当前版本 : 1.1.1"]];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    _tableView.delegate = self;
    _tableView.dataSource = self;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BSSettingTableViewCell *cell = nil;
         NSString *text =  _dataArray[indexPath.section][indexPath.row];
            cell = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][0];
            cell.fontSize.text = text;
        return cell;
        
    }
    else
    {
        BSSettingTableViewCell *cell3 = nil;
            cell3 = [[NSBundle mainBundle] loadNibNamed:@"BSSettingTableViewCell" owner:self options:nil][1];
        cell3.restTextLabel.text = _dataArray[indexPath.section][indexPath.row];
            cell3.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row < 4) {
            cell3.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        return cell3;
    }
    
    return nil;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _titleArray[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 || indexPath.section == 2)
    {
        return;
    }
    switch (indexPath.row)
    {
        case 0:
            
            [self creationAlte];
            break;
        case 1:
            [self RecommendToAFriend];
            break;
        case 2:
            
            [self AboutUs];
            break;
        case 3:
            [self privacyPolicy];
            break;
        case 4:
            break;

    }
}


- (void)creationAlte
{
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    CGFloat size = [self folderSizeAtPath:str];
    NSString *path = [NSString stringWithFormat:@"缓存大小为%.2fM,确定清除",size];
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:path delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [aler show];
    
}


//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];

    if ([btnTitle isEqualToString:@"确定"]) {
        NSString *str = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:str]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:str];
            for (NSString *fileName in childerFiles) {
                //如有需要，加入条件，过滤掉不想删除的文件
                NSString *absolutePath=[str stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
        [[SDImageCache sharedImageCache] cleanDisk];
    }
}

//分享
- (void)RecommendToAFriend
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55d8471be0f55a97c2003361"
                                      shareText:@"开怀一笑,瞬间戳中你的笑点,试试看吧"
                                     shareImage:[UIImage imageNamed:@"AppIcon29x29"]
                                shareToSnsNames:@[UMShareToQQ,UMShareToQzone,UMShareToSina,UMShareToWechatTimeline,UMShareToWechatSession]
                                       delegate:nil];
         [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];
    
    
    [UMSocialData defaultData].extConfig.qqData.url = @"http://www.budejie.com/budejie/?appinstall=1";
    [UMSocialData defaultData].extConfig.qzoneData.url = @"http://www.budejie.com/budejie/?appinstall=1";
    [UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://www.budejie.com/budejie/?appinstall=1";
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = @"http://www.budejie.com/budejie/?appinstall=1";
    [UMSocialData defaultData].extConfig.wechatSessionData.title = @"推荐一个搞笑的应用";
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = @"推荐一个搞笑的应用";
    [UMSocialData defaultData].extConfig.qqData.title = @"推荐一个搞笑的应用";
    [UMSocialData defaultData].extConfig.qzoneData.title = @"推荐一个搞笑的应用";
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = @"http://www.budejie.com/budejie/?appinstall=1";
    

    


}


//关于我们
- (void)AboutUs
{
    BSAboutUsTableViewController *aboutUsVC = [[BSAboutUsTableViewController alloc] init];
    [self.navigationController pushViewController:aboutUsVC animated:YES];
}
//隐私政策
- (void)privacyPolicy{
    BSPrivacyPolicyViewController *privacVC = [[BSPrivacyPolicyViewController alloc] init];
    [self.navigationController pushViewController:privacVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
